package com.bidevalution.support.compare;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * 文本读取实现类
 */
public class FileHandler {
	private Logger log = Logger.getLogger(getClass());

    /**
     * input:path
     * 将文件内容添加入list中，一句一条
     */
    public List<String> putFileToList(String path) throws IOException {

        List<String> docList = new ArrayList<String>();
        FileReader fr = null;
        BufferedReader br = null;
        String line;
        try {
            fr = new FileReader(path);
            br = new BufferedReader(fr);
            String text = "";
            while ((line = br.readLine()) != null) {
                if (StringUtils.isBlank(line)) {
                    continue;
                }
                String[] strArr;
                text += line;
                if (text.contains("。")) {
                    strArr = text.split("。");
                } else if (text.contains(";")) {
                    strArr = text.split(";");
                } else if (text.contains("；")) {
                    strArr = text.split("；");
                } else {
                    strArr = new String[1];
                    strArr[0] = line;
                }
                int i, n;
                n = strArr.length - 1;
                if (strArr.length > 1) {
                    text = strArr[n];
                }
                for (i = 0; i < n; i++) {
                    if (strArr[i] != null && StringUtils.isNotBlank(strArr[i].trim())) {
                        docList.add(strArr[i]);
                    }
                }
            }
            docList.add(text);
        } catch (Exception e) {
            log.error("文件为空或pdf文件解析错误", e);
        } finally {
            if (fr != null) {
                fr.close();
            }
            if (br != null) {
                br.close();
            }
        }
        return docList;
    }

    /**
     * input:path
     * output:List<String>
     * 读取词典信息，以list返回词典
     */
    public List<String> readDic(String path) {
        List<String> list = new ArrayList<String>();
        FileReader fr = null;
        BufferedReader br = null;
        String line;
        try {
            fr=new FileReader(path);
            br=new BufferedReader(fr);
            while ((line=br.readLine()) != null ) {
                StringTokenizer st = new StringTokenizer(line);
                while(st.hasMoreElements()){
                    list.add(st.nextToken());
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (Exception ex) {
                    log.error(ex.getMessage(), ex);
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (Exception ex) {
                    log.error(ex.getMessage(), ex);
                }
            }
        }

        return list;
    }

}
